package kpi.javatraining6.gnatiuk.src.tester.readers;

import kpi.javatraining6.gnatiuk.src.tester.question.Question;

import java.util.ArrayList;
import java.util.List;



import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;


public class QuestionSAXHandler extends DefaultHandler{
    private List<Question> questionsList;
    private String difficulty;
    private String question;
    private String questionType;
    private List<String> answers;
    private List<String> correctAnswers;
    private boolean questionElem;
    private boolean answerElem;


    private void initializeLists() {
        questionsList = new ArrayList<>();
        answers = new ArrayList<>();
        correctAnswers = new ArrayList<>();
    }

    @Override
    public void startElement (String uri, String localName,
                              String qName, Attributes attributes)  throws SAXException {
        switch (qName){
            case "questionlist":
                initializeLists();
                break;
            case "question":
                questionElem = true;
                break;
            case"questionItem":
                questionType = attributes.getValue("type");
                difficulty = attributes.getValue("complexity");
                break;
            case "answer":
                answerElem = true;
                if (attributes.getValue("correct").equals("true")) {
                    correctAnswers.add(null);
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void characters (char[] ch, int start,
                            int length) throws SAXException {
          if (questionElem) {
              question = new String(ch, start, length);
              questionElem = false;
          }

          if (answerElem){
                  answers.add(new String(ch, start, length));
                  if (correctAnswers.size() > 0) {
                      int lastElemIndex = correctAnswers.size() - 1;
                      if (correctAnswers.get(lastElemIndex) == null) {
                        correctAnswers.set(lastElemIndex, new String(ch, start, length));
                      }
                  }
              answerElem = false;
          }
    }

    @Override
    public void endElement (String uri, String localName,
                            String qName) throws SAXException {
        if (qName.equals("questionItem")) {
            QuestionValues questionValues = new QuestionValues(difficulty, question, answers, correctAnswers);
            try {
                Question questionItem = QuestionFactory.getQuestion(questionType, questionValues);
                questionsList.add(questionItem);
            } catch (ClassNotFoundException e) {
                System.out.println("wrong arguments to factory");
                System.exit(-1);
            }
            answers = new ArrayList<>();
            correctAnswers = new ArrayList<>();
        }
    }

    public List<Question> getQuestions() {
        return questionsList;
    }
}
